package com.xht.utils;

import com.xht.domain.entity.Article;
import com.xht.vo.HotArticleVo;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtils {
    private BeanCopyUtils() {}


    //copy单个对象
    public static <T>T copyBean(Object source,Class<T> clazz){

        //创建目标对象
        T result = null;
        try{
            result = clazz.newInstance();
            //实现属性copy
            BeanUtils.copyProperties(source,result);

        }
        catch (Exception e){
            e.printStackTrace();
        }
        //返回结果
        return result;
    }

    //copy列表
    public static <O,V> List<V> copyBeanList(List<O> list,Class<V> clazz){

        List<V> collect = list.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
        return collect;


    }


    public static void main(String[] args) {
        Article article = new Article();
        article.setId(1L);
        article.setTitle("ss");

        HotArticleVo hotArticleVo = copyBean(article,HotArticleVo.class);
        System.out.println(hotArticleVo);
    }
}
